package tablization;

import java.util.Hashtable;
import java.util.Vector;
import dbmanager.DBManager;

/**
 *  @author Shweta
 **/
public class TableUpdation extends SubTable 
{
	
	
	public Vector<Object> oldVector = new Vector<Object>() ;
	public Vector<Object> newVector = new Vector<Object>() ;
		
	public TableUpdation(DBManager sqlDB)
	{
		super.setDBManager(sqlDB);
	}
	
	
	/**
	 * this function updates value of column of the table
	 * @param oldPropertyVector - contains column names & their old values whose values to be updated
	 * @param newPropertyVector - contains new values for columns to be updated
	 * @return true if table is updated otherwise false
	 */
	@SuppressWarnings("unchecked")
	public boolean getUpdate(Vector<Object> oldPropertyVector, Vector<Object> newPropertyVector) 
	{
		boolean flag = false;
		oldVector = oldPropertyVector ;
		newVector = newPropertyVector ;
		
		Hashtable<Object,Object> hasht = (Hashtable<Object, Object>)oldVector.get(0);
		
		String midName = hasht.get("mid").toString();
		
		//get array of tables for given mid name
		Hashtable<Object,Object> arrayHash = new Hashtable<Object, Object>();				
		
		arrayHash.put(PID_NAME, "tablization");
		arrayHash.put(keyName, "mid");
		arrayHash.put(valueName, midName);
		
		String [] tableArray= getArray(arrayHash);
		
		if(tableArray.length != 0)
		{
			for(int i=0;i<oldVector.size();i++)
			{				
				Hashtable<Object,Object> tempOldHash = (Hashtable<Object, Object>)oldVector.get(i);
				String column = tempOldHash.get("pid").toString();
				
				//get array of tables having given column
				arrayHash = new Hashtable<Object, Object>();				
				
				arrayHash.put(PID_NAME, "column");
				arrayHash.put(keyName, "pv");
				arrayHash.put(valueName, column);
				
				String [] columnTableArray= getArray(arrayHash);
				
				//get tablename to update values of given column
				String tablename = getTableName(tableArray,columnTableArray); 
				
				if(!tablename.equals(""))
				{
					
					//update given table					
					column = gin.getItem(column);
					String oldvalue = getColumnValue(tempOldHash);
					
					Hashtable<Object, Object> oldHash = new Hashtable<Object, Object>();
					oldHash.put("id",midName);
					oldHash.put(tablename +"."+column , oldvalue);
					
					Hashtable<Object,Object> tempNewHash = (Hashtable<Object, Object>)newVector.get(i);
					String newvalue = getColumnValue(tempNewHash);
					
					Hashtable<Object, Object> newHash = new Hashtable<Object, Object>();
					newHash.put("id",midName);
					newHash.put(tablename +"."+column , newvalue);
					
					int x = sqlDB.getUpdate(tablename, newHash, oldHash);
					if(x>0)
					{
						oldVector.remove(i);
						newVector.remove(i);
						try	{
							i--;
						} catch (Exception e) {
							i=0;
						}
						flag = true;
					}
					else
					{
						//flag = false;
						System.out.println("Value doesn't exist");
					}
				}
				else
					flag = false;
			}
		}
		else
		{
			flag = false;
			System.out.println("Table doesn't exist");
		}
		
	
		return flag;		
	}
	
	
	
	public static void main(String[] args)
	{
		DBManager sqldb = new DBManager();
		sqldb.getConnect();
		
		TableUpdation tu = new TableUpdation(sqldb);		
		
		Vector<Object> oldVector = new Vector<Object>();
		Vector<Object> newVector = new Vector<Object>();
		
		Hashtable<Object,Object> hasht = new Hashtable<Object, Object>();
		
		hasht.put("mid","aaa501");
		hasht.put("pid","reference");
		hasht.put("pv","BCA");
		
		oldVector.add(tu.convertInMasterId(hasht));
		
		hasht = new Hashtable<Object, Object>();
		
		hasht.put("mid","aaa501");
		hasht.put("pid","reference");
		hasht.put("pv","DCA");
		
		newVector.add(tu.convertInMasterId(hasht));		
		String type = "human2";
		boolean flag = false;
		
		if(tu.checkTableExistence(type) == true)
		{
			flag = tu.getUpdate(oldVector,newVector);
		}
		if(flag == true)
		{
			System.out.println("Table Column Updated successfully");
			if(tu.oldVector.size() != 0 & tu.newVector.size() != 0)
				//call pl getUpdate
				System.out.println();
		}
	}
	
}
